import { QL } from "iflux2";
import { fromJS } from "immutable";
import { getSystemCode } from "../common/const-value";

const systemCode = getSystemCode().toLowerCase();
const SystemPageInfo = {
  d2p: {
    pc: {
      type: "b",
      platform: "pc",
      label: "云订货PC端"
    },
    app: {
      type: "a",
      platform: "app",
      label: "云订货APP端"
    },
    weixin: {
      type: "x",
      platform: "weixin",
      label: "云订货微信端"
    }
  },
  d2c: {
    pc: {
      type: "c",
      platform: "pc",
      label: "PC店铺"
    },
    weixin: {
      type: "w",
      platform: "weixin",
      label: "微信店铺"
    }
  }
};
const DefaultTitle = {
  d2p: "我的订货",
  d2c: "我的店铺"
};

export const pageInfoQL = QL("pageInfoQL", [
  "domainList",
  "pageInfo",
  (domainList, pageInfo) => {
    return fromJS(SystemPageInfo[systemCode]).map((page, platform) => {
      return page
        .set("release", pageInfo.get(platform))
        .set("pageCode", pageInfo.getIn([platform, "pageCode"]))
        .set("pageType", pageInfo.getIn([platform, "pageType"]))
        .set(
          "title",
          pageInfo.getIn([platform, "title"]) || DefaultTitle[systemCode]
        )
        .set(
          "domainUrl",
          domainList
            .filter(
              domain =>
                domain.get("dtype") == page.get("type") &&
                domain.get("domainDefault") == "Y"
            )
            .getIn([0, "domainUrl"])
        );
    });
  }
]);
